<?php
/**
 * 新浪 SDK callback
 */
//设置时 init.php的session失效 使用新浪API的session配置 用于确保存放路径正确
//sission设置
//设置一个存放目录
$savePath=str_replace("\\","/",dirname(__FILE__))."/../../session_save/";
//保存12小时
$lifeTime = 43200;//保存12小时 (12*3600)
//保存路径
session_save_path($savePath);
//生命周期
session_set_cookie_params($lifeTime);
session_start();
//设置使 init.php的session失效 使用qqConnectAPI的session配置
$new_session_set=1;

//全局变量
$IN_defaultpl="default";
include(dirname(__FILE__)."/../../index.inc.php");//首页配置 在config.db.php已经配置了新浪 api参数
require_once(dirname(__FILE__)."/../../includes/class.sina.api/saetv2.ex.class.php");


$o = new SaeTOAuthV2(WB_AKEY,WB_SKEY);
if(isset($_REQUEST['code'])){
	$keys = array();
	$keys['code'] = $_REQUEST['code'];
	$keys['redirect_uri'] = WB_CALLBACK_URL;
	try {
		$token = $o->getAccessToken('code', $keys ) ;
	}
	catch(OAuthException $e){
		return $rs = $e->getMessage();
		$fun->msg("新浪授权注册失败,正在跳转到网站注册","./../../../".$list_url['register'],2,$title="注册提示",$back_color="f78a12",$link_url="../../");
	}
}

if($token){
	$_SESSION['token']=$token;
	setcookie( 'weibojs_'.$o->client_id,http_build_query($token));
	$access_tonken=$_SESSION['token']['access_token'];
	//新浪授权成功 获取uid
	$c = new SaeTClientV2(WB_AKEY,WB_SKEY,$access_tonken);
	$_sina_uid_get = $c->get_uid();
	$_sina_uid = $_sina_uid_get['uid'];//新浪的用户id
	
	//判断是否绑定了用户
	$sql="SELECT u.`uid`, u.`uname`, u.`realname`, u.`ncname`, u.`password`, u.`addtime`, u.`lastlogintime`, u.`logintime`,u.`login_num`,u.`status` FROM `".$tbprefix."user` AS u INNER JOIN `".$tbprefix."user_token` AS t ON t.`uid`=u.`uid` WHERE t.`qq_sina_uid`='{$_sina_uid}' AND t.`type_form`='2' AND u.`is_valid`='1' AND 1";
	$rs=$db->get_one($sql);
	//判断是否已经绑定用户信息 已经绑定直接登录
	if(!empty($rs['uid'])){
		if($rs['status']==1){
			//login success save login message
			$dataArray['`logintime`']=$m_now_time;				//登录时间
			$dataArray['`lastlogintime`']=$rs['logintime'];		//最后登录时间
			$dataArray['`login_num`']=$rs['login_num']+1; 		//登录次数
			$dataArray['`modify_ip`']=$fun->get_web_ip(); 		//登录IP
			$db->update("`".$tbprefix."user`",$dataArray,$condition=" `uid`='".$rs['uid']."' AND 1");
			//update qq access_token time 这里是新浪绑定 授权时间更新
			$dataArr['changetime']=$m_now_time;
			$dataArr['access_token']=$access_tonken;
			$db->update("`".$tbprefix."user_token`",$dataArr,$condition=" `uid`='".$rs['uid']."' AND `qq_sina_uid`='{$_sina_uid}' AND `type_form`='2' AND 1");
		
			//存储登录的基本信息
			@$_SESSION['web_user_uid']=$fun->authcode($rs['uid'],'ENCODE',$pwdconstant,$expiry=0);
			 
			/* shopnc login */
		
			$shoplogin = SHOPNC_DOMAIN."index.php?act=login&user_name={$login_user}&password={$login_password}&form_submit=ok";
			echo '<iframe height=0 width=0 style="display:none;" src="'.$shoplogin.'"></iframe>';
			
			/* shopnc login ok*/
			
			/* discuz login */
			
			$discuzlogin = DISCUZ_DOMAIN."member.php?mod=logging&action=login&loginsubmit=yes&handlekey=login&username={$login_user}&password={$login_password}";
			echo '<iframe height=0 width=0 style="display:none;" src="'.$discuzlogin.'"></iframe>';
			
			/* discuz login */
			 
			//来自的连接 用于三网合并 1首页 2论坛 3商城
			$url_from=isset($_REQUEST["from"])?$_REQUEST["from"]:"1";
			$backurl = $fun->back_to_which_from($url_from);
			//$fun->msg("登陆成功,正在前往首页...",$backurl,3,$title="登录提示",$back_color="f78a12",$link_url="../../");
			echo "<script  type='text/javascript' language='javascript'>window.location.href='{$backurl}';</script>";
			exit();
		}else{
			$fun->msg("您的用户名出现异常,我们已经对您的用户名做出冻结处理,您无法登录","./../../".$list_url['login'],2,$title="登录提示",$back_color="f78a12",$link_url="./../../");
		} 
	}else{
		//授权成功 跳转到新浪授权绑定页面 以下做授权access_token更新操作
		$sina_token_sql="SELECT t.id,t.`uid` FROM  `".$tbprefix."user_token` AS t WHERE t.`qq_sina_uid`='{$_sina_uid}' AND t.`type_form`='2' AND 1";
		$sina_rs=$db->get_one($sina_token_sql);
		if($sina_rs['id']){
			//存在授权 更新access_token
			//update qq access_token time 这里是新浪绑定 授权时间更新
			$dataArr['`changetime`']=$m_now_time;
			$dataArr['access_token']=$access_tonken;
			$dataArr['type_form']=2;
			$db->update("`".$tbprefix."user_token`",$dataArr,$condition=" `id`='{$sina_rs['id']}' AND 1");
			$sina_token_id=$sina_rs['id'];
		}else{
			//不存在授权 新增一条
			//保存授权信息
			$dataArr['access_token']=$access_tonken;
			$dataArr['qq_sina_uid']=$_sina_uid;
			$dataArr['addtime']=$m_now_time;
			$dataArr['type_form']=2;
			$db->insert($table="`".$tbprefix."user_token`",$dataArr);
			$sina_token_id=$db->insert_id();
		}
		
		//存储保存的token表主键 用于绑定
		@$_SESSION['web_sina_token_id']=$fun->authcode($sina_token_id,'ENCODE',$pwdconstant,$expiry=0);
		
		//跳转到QQ绑定
		echo "<script  type='text/javascript' language='javascript'>window.location.href='./../../".$list_url['bindsina']."';</script>";
		exit();
	}
	
}else {
	$fun->msg("新浪授权注册失败,正在跳转到网站注册","./../../".$list_url['register'],2,$title="注册提示",$back_color="f78a12",$link_url="./../../");
}
?>
